<template>
  <div class="app-container" v-loading="loading">

    <div :class="className">
      <div style="padding-left: 5%;margin-top: 2%;">
        <el-button type="primary" v-if="pervious" class="text-center" @click="handPrevious()"> 上一个 </el-button>
      </div>

      <div style="padding-left: 30%;">
        <h2 class="text-center">{{ paperData.title }}</h2>
        <span class="text-center">考生姓名：{{ paperData.realName }}</span>
      </div>
      <div style="padding-left: 25%;margin-top: 2%;">
        <el-button v-if="next" type="warning" class="text-center" @click="handNext()"> 下一个 </el-button>
      </div>

    </div>
    <div>
      <el-dialog title="存在抄袭嫌疑" :visible.sync="gridData">
        <el-table :data="gridData">
          <el-table-column property="userNameA" label="抄袭人" width="150"></el-table-column>
          <el-table-column property="userNameB" label="对比人" width="200"></el-table-column>
          <el-table-column property="examName" label="考试名字"></el-table-column>
          <el-table-column property="content" label="试题名字"></el-table-column>
        </el-table>
      </el-dialog>
    </div>
    <el-row :gutter="24">
      <el-col :span="8" class="text-center"><span class="hidden">e</span><el-button v-if="pervious" type="primary"
          @click="handPrevious()"> 上一个
        </el-button> </el-col>
      <el-col :span="8">
        <h2 class="text-center">{{ paperData.title }}</h2>
        <p class="text-center" style="color: #666">{{ paperData.createTime }}</p>
      </el-col>
      <el-col :span="8" class="text-center"><el-button type="warning" v-if="next" @click="handNext()"> 下一个
        </el-button></el-col>
    </el-row>


    <el-row :gutter="24" style="margin-top: 50px">

      <el-col :span="8" class="text-center">
        考生姓名：{{ paperData.realName }}
      </el-col>

      <el-col :span="8" class="text-center">
        考试用时：{{ paperData.userTime <= 0 ? "少于1" : paperData.userTime }}分钟 </el-col>

          <el-col :span="8" class="text-center">
            考试得分：{{ paperData.userScore }}
          </el-col>

    </el-row>

    <el-card style="margin-top: 20px">

      <div v-for="(item, index) in paperData.quList" :key="item.id" class="qu-content">
        <p>{{ item.sort + 1 }}.{{ item.content }}（得分：
          <span class="span-color" v-if="quState[index]" @click="changeSate(index)">{{ item.actualScore }}</span>
          <el-input-number v-else @blur="changeSate(index)" :id="index" v-model="item.actualScore" :max="item.score" :min="0"
            style="width:120px"></el-input-number>）


        </p>
        <p v-if="item.image != null && item.image != ''">
          <el-image :src="item.image" style="max-width:100%;" />
        </p>
        <div v-if="item.quType === 1 || item.quType === 3">
          <el-radio-group v-model="radioValues[item.id]">
            <el-radio v-for="an in item.answerList" :label="an.id" disabled="true">
              {{ an.abc }}.{{ an.content }}
              <div v-if="an.image != null && an.image != ''" style="clear: both">
                <el-image :src="an.image" style="max-width:100%;" />
              </div>
            </el-radio>
          </el-radio-group>

          <el-row :gutter="24">

            <el-col :span="12" style="color: #24da70">
              正确答案：{{ radioRights[item.id] }}
            </el-col>

            <el-col v-if="!item.answered" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：未答
            </el-col>

            <el-col v-if="item.answered && !item.isRight" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：{{ myRadio[item.id] }}
            </el-col>

            <el-col v-if="item.answered && item.isRight" :span="12" style="text-align: right; color: #24da70;">
              答题结果：{{ myRadio[item.id] }}
            </el-col>

          </el-row>

        </div>

        <div v-if="item.quType === 5">

          <el-row :gutter="24">

            <el-col :span="12">
              我的回答：{{ item.answer }}
            </el-col>
            <el-col :span="12">
              正确答案：{{ item.answerList[0].content }}
            </el-col>
          </el-row>

        </div>

        <div v-if="item.quType === 4">

          <el-row :gutter="24">

            <el-col :span="12">
              我的回答：{{ item.answer }}
            </el-col>

          </el-row>

        </div>

        <div v-if="item.quType === 2">
          <el-checkbox-group v-model="multiValues[item.id]">
            <el-checkbox v-for="an in item.answerList" :key="an.id" :label="an.id" disabled="true">{{ an.abc }}.{{
              an.content }}
              <div v-if="an.image != null && an.image != ''" style="clear: both">
                <el-image :src="an.image" style="max-width:100%;" />
              </div>
            </el-checkbox>
          </el-checkbox-group>

          <el-row :gutter="24">

            <el-col :span="12" style="color: #24da70">
              正确答案：{{ multiRights[item.id].join(',') }}
            </el-col>

            <el-col v-if="!item.answered" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：未答
            </el-col>

            <el-col v-if="item.answered && !item.isRight" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：{{ myMulti[item.id].join(',') }}
            </el-col>

            <el-col v-if="item.answered && item.isRight" :span="12" style="text-align: right; color: #24da70;">
              答题结果：{{ myMulti[item.id].join(',') }}
            </el-col>

          </el-row>
        </div>

      </div>

    </el-card>

  </div>
</template>

<script>

import { paperResult } from '@/api/paper/exam'
import { listPaper, updateState } from '@/api/paper/paper'
import { similarityByExamId } from '@/api/similarity/similarity'
export default {
  data() {
    return {

      className: "no-display",
      //保存所有本次考试的待批改的试卷id
      paperDataList: {},
      quState: [],
      // 试卷ID
      paperId: '',
      examId: '',
      paperData: {
        quList: []
      },
      radioValues: {},
      multiValues: {},
      radioRights: {},
      multiRights: {},
      myRadio: {},
      myMulti: {},

      gridData:[],

      //按钮显示
      pervious: false,
      next: false,
      loading: false,
    }
  },
  created() {
    const id = this.$route.params.id
    const examId = this.$route.params.examId

    if (typeof id !== 'undefined' && typeof examId !== 'undefined') {
      this.paperId = id
      this.examId = examId
      this.fetchData(id)
      this.getPaperListByExamId(examId)
      this.similarity(examId);
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)

  },
  methods: {
    similarity(examId){
      similarityByExamId({examId:examId}).then(res =>{
        this.gridData = res.data;
      })
    },
    async fetchData(id) {
      this.loading = true
      const params = { id: id }
      await paperResult(params).then(response => {
        // 试卷内容
        this.paperData = response.data
        this.quState = this.paperData.quList.map(() => true);
        // 填充该题目的答案
        this.paperData.quList.forEach((item) => {

          let radioValue = ''
          let radioRight = ''
          let myRadio = ''
          const multiValue = []
          const multiRight = []
          const myMulti = []

          item.answerList.forEach((an) => {
            // 用户选定的
            if (an.checked) {
              if (item.quType === 1 || item.quType === 3) {
                radioValue = an.id
                myRadio = an.abc
              } else {
                multiValue.push(an.id)
                myMulti.push(an.abc)
              }
            }

            // 正确答案
            if (an.isRight) {
              if (item.quType === 1 || item.quType === 3) {
                radioRight = an.abc
              } else {
                multiRight.push(an.abc)
              }
            }
          })

          this.multiValues[item.id] = multiValue
          this.radioValues[item.id] = radioValue

          this.radioRights[item.id] = radioRight
          this.multiRights[item.id] = multiRight

          this.myRadio[item.id] = myRadio
          this.myMulti[item.id] = myMulti


        })
        const that = this;
        setTimeout(function () {
          that.loading = false
        }, 1000)

      })

    },
    handleScroll() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

      if (scrollTop > 50) {

        this.className = "always-top";

      } else {

        this.className = "no-display";

      }

    },
    handNext() {

      var i = this.paperDataList.findIndex(item => item === this.paperId);
      if (typeof i != "undefined") {
        let oldPaperId = this.paperId;
        this.paperId = this.paperDataList[i + 1]
        this.handSave(this.paperDataList[i + 1], oldPaperId)
      }
    },
    handPrevious() {
      var i = this.paperDataList.findIndex(item => item === this.paperId);
      if (typeof i != "undefined") {
        let currentId = this.paperId;
        this.paperId = this.paperDataList[i - 1]
        this.handSave(this.paperDataList[i - 1], currentId)
      }
    },
    handSave(paperId, oldPaperId) {

      if (this.paperId === this.paperDataList[0]) {
        this.pervious = false;
      } else {
        this.pervious = true;
      }
      if (this.paperId === this.paperDataList[this.paperDataList.length - 1]) {
        this.next = false;
      } else {
        this.next = true;
      }

      updateState({ id: oldPaperId }).then(res => {
        //TODO:向后端更新数据
        this.fetchData(paperId);
      })

    },
    changeSate(id) {
      // if(this.quState[id]){
      //   console.log(document.getElementById(id));
      // }
      this.$set(this.quState, id, !this.quState[id])
    },

    getPaperListByExamId(id) {
      const params = {
        currentPage: 1,
        size: 100,
        examId: id,
        state: 2
      }
      listPaper(params).then(res => {
        if (res.data != null) {
          this.paperDataList = res.data.records.map((item) => item.id)
          if (this.paperId === this.paperDataList[0]) {
            this.pervious = false;
          } else {
            this.pervious = true;
          }
          if (this.paperId === this.paperDataList[this.paperDataList.length - 1]) {
            this.next = false;
          } else {
            this.next = true;
          }
        }

      })
    }
  }
}
</script>

<style scoped>
.always-top {
  display: flex;
  z-index: 999;
  width: 100%;
  height: 100px;
  position: fixed;
  top: 5px;
  background-color: blanchedalmond;
  transition: height 2s;
  opacity: 0.9;
}

.no-display {
  display: none;
}

.qu-content {

  border-bottom: #eee 1px solid;
  padding-bottom: 10px;

}

.qu-content div {
  line-height: 30px;
}

.hidden {
  color: white;
}

.el-checkbox-group label,
.el-radio-group label {
  width: 100%;
}

.card-title {
  background: #eee;
  line-height: 35px;
  text-align: center;
  font-size: 14px;
}

.card-line {
  padding-left: 10px
}

.card-line span {
  cursor: pointer;
  margin: 2px;
}

.span-color {
  color: rgb(234, 13, 13);
  cursor: pointer;
}
</style>

